/*
 * 创建日期 2009-12-7
 *
 * 成都天和软件公司
 * 电话：028-85425861
 * 传真：028-85425861-8008
 * 邮编：610041 
 * 版权所有
 */
package  org.fantasy.common.grid.report;

import  org.fantasy.common.db.bean.ReportRow;
import  org.fantasy.common.grid.bean.Column;
import  org.fantasy.common.grid.bean.ReportParam;
import  org.fantasy.common.grid.custom.CellCustom;
import  org.fantasy.common.grid.custom.RowCustom;
import  org.fantasy.common.util.StringUtil;

import java.util.List;
import java.util.Set;

/**
 *  抽象报表构造器
 * @author: 王文成
 * @version: 1.0
 * @since 2009-12-7
 */
public abstract class AbstractReportBuilder implements ReportBuilder{
    
    protected ReportParam param;
    
    protected RowCustom rowCustom;
    
    protected CellCustom cellCustom;
    
    protected List<List<Column>> tableHeads;
    
    protected List<Column> tableFields;
    
    protected Set<String> hideHeads;
    
    protected String rowHeight;
    
    protected List<ReportRow> resultList;
    
    public AbstractReportBuilder(ReportParam report) throws Exception{
        this.param = report;
        init();
    }
    
    /**
     * 初始化数据
     * @throws Exception
     */
    @SuppressWarnings("all")
    private void init() throws Exception{
        this.rowCustom = param.getRowCustom();
        this.cellCustom = param.getCellCustom();
        this.rowHeight = param.getRowHeight();
        this.tableHeads = param.getTableHeads();
        this.tableFields = param.getTableFields();
        this.hideHeads = param.getHideHeads();
        this.resultList = param.getDataList();
    }
    
    /**
     * 取得报表
     * @return
     */
    public abstract Object getReport() throws Exception;
    
    /**
     * 创建报表头
     * @return
     * @throws Exception
     */
    protected abstract StringBuffer buildHead() throws Exception;
    
    /**
     * 创建报表体
     * @return
     * @throws Exception
     */
    protected abstract StringBuffer buildBody() throws Exception;
    
    /**
     * 取得样式
     * 
     * @param i
     * @return
     */
    protected String getRowStyle(ReportRow row, int i) throws Exception{
        StringBuffer html = new StringBuffer();
        String style = rowCustom.getStyle(row,i);
        String className = rowCustom.getClassName(row,i);
        if (StringUtil.isValid(style) || StringUtil.isValid(className)) {
            if (StringUtil.isValid(style)) {
                html.append(" style='" + style + "'");
            }
            if (StringUtil.isValid(className)) {
                html.append(" class='" + className + "'");
            }
        }
        return html.toString();
    }
    
    /**
     * 取得样式
     * 
     * @param i
     * @return
     */
    protected String getCellStyle(ReportRow row, Column col)  throws Exception  {
        StringBuffer html = new StringBuffer();
        String style = cellCustom.getStyle(row,col);
        String className = cellCustom.getClassName(row,col);
        if (StringUtil.isValid(style) || StringUtil.isValid(className)) {
            if (StringUtil.isValid(style)) {
                html.append(" style='" + style + "'");
            }
            if (StringUtil.isValid(className)) {
                html.append(" class='" + className + "'");
            }
        }
        return html.toString();
    }
}
